package com.zznj.xzjc.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zznj.xzjc.R;
import com.zznj.xzjc.bean.FileModel;
import com.zznj.xzjc.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import androidx.fragment.app.Fragment;

public class FragmentActivityFile extends Fragment {
	private View RootView = null;
	private Context mContext;
	private ListView mListView;
	private MyAdapter myAdapter;
	private List<FileModel> fileList;
	private OnFileChoosedListener mListener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (container != null) {
			this.mContext = getActivity();
			RootView = inflater.inflate(R.layout.gyss_fragment_file, container, false);
			initView(RootView);
		}
		return RootView;
	};

	private void initView(View rootView) {
		mListener = (OnFileChoosedListener) getActivity();
		String path = getArguments().getString("filepath");
		if (path == null || "".equals(path)) {
			// 则使用SD卡路�?
			path = Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		mListView = (ListView) rootView.findViewById(R.id.fileList);
		myAdapter = new MyAdapter(mContext);
		mListView.setAdapter(myAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mListener.onChoosedListener(myAdapter.getItem(position));
			}
		});
		initData(path);
	}

	private void initData(String path) {
		File[] files = new File(path).listFiles();
		// 按文件名正序排序
		Arrays.sort(files, new Comparator<File>() {
			@Override
			public int compare(File lFile, File rFile) {
				return lFile.getName().compareTo(rFile.getName());
			}
		});
		if (files.length != 0) {
			fileList = new ArrayList<FileModel>();
			List<FileModel> list = new ArrayList<FileModel>();
			for (File file : files) {
				if (file.isDirectory()) {
					// 添加�?��目录类型File
					fileList.add(new FileModel(file.getPath(), file.getName(), true));
				} else {
					String fileName = file.getName();
					// 判断只显示正常的“文件名.扩展名�?形式的文件，不显示特殊的文件，如只有扩展名没有文件名部分�?扩展名�?，只有文件名部分而没有扩展名部分“文件名�?
					if (fileName.indexOf(".") > 0 && (fileName.indexOf(".") != fileName.length() - 1)) {
						list.add(new FileModel(file.getPath(), file.getName(), false));
					}
				}
			}
			// 添加�?��文件类型File
			fileList.addAll(list);
			if (fileList.size() != 0) {
				myAdapter.setList(fileList);
			}
		}
	}

	private class MyAdapter extends BaseAdapter {
		private Context pContext;
		private List<FileModel> fileList;

		public MyAdapter(Context context) {
			this.pContext = context;
			this.fileList = new ArrayList<FileModel>();
		}

		public void setList(List<FileModel> lists) {
			this.fileList = lists;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return fileList.size();
		}

		@Override
		public FileModel getItem(int position) {
			return fileList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			FileHolder fileHolder = null;
			if (convertView == null) {
				fileHolder = new FileHolder();
				convertView = LayoutInflater.from(pContext).inflate(R.layout.gyss_item_file, null);
				fileHolder.mIcon = (ImageView) convertView.findViewById(R.id.fileIcon);
				fileHolder.mText = (TextView) convertView.findViewById(R.id.itemFileName);
				convertView.setTag(fileHolder);
			} else {
				fileHolder = (FileHolder) convertView.getTag();
			}
			fileHolder.mText.setText(fileList.get(position).getFileName());
			fileHolder.mIcon.setBackgroundResource(fileList.get(position).isFileFolder() ? R.mipmap.icon_file_folder : FileUtils.getIcon(fileList.get(position).getFileName()));
			return convertView;
		}

		public final class FileHolder {
			ImageView mIcon;
			TextView mText;
		}
	}

	public interface OnFileChoosedListener {
		public void onChoosedListener(FileModel fileModel);
	}
}
